import urls from '/util/urls'
import request from '/util/request'
const app = getApp()

Page({
  data: {
    id: '',
    // 厅局编码
    institutionCode: '',
    // 厅局名称
    institutionName: '',
    // 编码
    code: '',
    // 执行人名称
    userName: '',
    // 执行人编码
    userCode: '',
    // 开始时间
    startDateTime: '',
    // 结束时间
    endDateTime: '',
    // 解决方法
    solution: '',
    // 存在问题
    problem: '',
    // 巡检项列表
    itemValues: [],
    // 巡检类型
    inspectionType: {}
  },
  onLoad(options) {
    let that = this
    // 传递的参数
    const id = options.id
    // 加载每日巡检的详细信息
    request.get(urls.inspectionRegularDetailUrl(id), function (data) {
      that.setData({
        id: data.id,
        // 厅局编码
        institutionCode: data.institutionCode,
        // 厅局名称
        institutionName: data.institutionName,
        // 编码
        code: data.code,
        // 执行人名称
        userName: data.userName,
        // 执行人编码
        userCode: data.userCode,
        // 开始时间
        startDateTime: data.startDateTime,
        // 结束时间
        endDateTime: data.endDateTime,
        // 解决方法
        solution: data.solution,
        // 存在问题
        problem: data.problem,
        // 巡检项列表
        itemValues: data.itemValues.map(it => {
          if (data.status === 'notStarted') {
            if (it.item.dataType === 'select') {
              it.value = '正常'
            }
          }
          if (it.item.dataType === "number") {
            let arr = it.item.candidateValueArr.sort()
            it.item.candidateValueArr = [Number(arr[0]), Number(arr[arr.length - 1])]
          }
          return it
        })
      })
    })

    // 巡检类型
    request.get(urls.inspectionTypeDictUrl(), function (data) {
      const obj = {}
      data.forEach(element => {
        obj[element.code] = element.text
      });
      that.setData({
        inspectionType: obj
      })
    })
  },
  // 获取巡检项名称
  getInspectionTypeName(code) {
    const { inspectionTypeList } = this.data
    const one = inspectionTypeList.find(e => e.code == code)
    return one ? one.text : ''
  },
  // 输入框发生变化
  onItemInput(e) {
    this.setData({
      [e.target.dataset.field]: e.detail.value,
    });
  },
  // 清除输入框
  onClear(e) {
    this.setData({
      [e.target.dataset.field]: '',
    });
  },
  // 选择值的时候发生变化
  radioChange(e) {
    this.setData({
      ['itemValues[' + e.target.dataset.field + '].value']: e.detail.value
    });
  },
  numInput(e) {
    this.setData({
      ['itemValues[' + e.target.dataset.field + '].value']: e.detail.value
    });
  },
  numClear(e) {
    this.setData({
      ['itemValues[' + e.target.dataset.field + '].value']: ''
    });
  },
  forInput(e) {
    this.setData({
      ['itemValues[' + e.target.dataset.field + '].reason']: e.detail.value
    });
  },
  forClear(e) {
    this.setData({
      ['itemValues[' + e.target.dataset.field + '].reason']: ''
    });
  },
  // 表单校验
  validData(formData) {
    const values = formData.itemValues.filter(e => !e.value)
    if (values.length > 0) {
      const content = values.map(e => e.item.item).join()
      dd.alert({ content: content + '没有填写巡检结果' })
      return false
    }
    return true
  },
  onSubmit() {
    const that = this
    const {
      id,
      institutionCode,
      institutionName,
      code,
      userName,
      userCode,
      startDateTime,
      endDateTime,
      solution,
      problem,
      itemValues
    } = this.data

    const formData = {
      id,
      institutionCode,
      institutionName,
      code,
      userName,
      userCode,
      startDateTime,
      endDateTime,
      solution,
      problem,
      itemValues
    }

    // if(!that.validData(formData)) {
    //   return
    // }

    dd.showLoading({
      content: '提交中...',
    })
    request.put(urls.inspectionRegularUrl(id), formData, () => {
      dd.hideLoading()
      dd.showToast({
        type: 'success',
        content: '提交成功',
        duration: 2000,
        success: () => {
          dd.redirectTo({
            url: '/pages/inspection/table/table?type=regular'
          })
        },
      })
    }, () => {
      dd.hideLoading()
      dd.showToast({
        type: 'fail',
        content: '提交失败',
        duration: 2000,
      })
    })
  }
});
